<!doctype html>

<html>
<head>
  <link rel="shortcut icon" href="static/images/favicon.ico" type="image/x-icon">
  <title>string_test.js (Closure Library API Documentation - JavaScript)</title>
  <link rel="stylesheet" href="static/css/base.css">
  <link rel="stylesheet" href="static/css/doc.css">
  <link rel="stylesheet" href="static/css/sidetree.css">
  <link rel="stylesheet" href="static/css/prettify.css">

  <script>
     var _staticFilePath = "static/";
     var _typeTreeName = "goog";
     var _fileTreeName = "Source";
  </script>

  <script src="static/js/doc.js">
  </script>


  <meta charset="utf8">
</head>

<body onload="grokdoc.onLoad();">

<div id="header">
  <div class="g-section g-tpl-50-50 g-split">
    <div class="g-unit g-first">
      <a id="logo" href="index.html">Closure Library API Documentation</a>
    </div>

    <div class="g-unit">
      <div class="g-c">
        <strong>Go to class or file:</strong>
        <input type="text" id="ac">
      </div>
    </div>
  </div>
</div>

<div class="clear"></div>

<h2><a href="closure_goog_string_string_test.js.html">string_test.js</a></h2>

<pre class="prettyprint lang-js">
<a name="line1"></a>// Copyright 2006 The Closure Library Authors. All Rights Reserved.
<a name="line2"></a>//
<a name="line3"></a>// Licensed under the Apache License, Version 2.0 (the &quot;License&quot;);
<a name="line4"></a>// you may not use this file except in compliance with the License.
<a name="line5"></a>// You may obtain a copy of the License at
<a name="line6"></a>//
<a name="line7"></a>//      http://www.apache.org/licenses/LICENSE-2.0
<a name="line8"></a>//
<a name="line9"></a>// Unless required by applicable law or agreed to in writing, software
<a name="line10"></a>// distributed under the License is distributed on an &quot;AS-IS&quot; BASIS,
<a name="line11"></a>// WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
<a name="line12"></a>// See the License for the specific language governing permissions and
<a name="line13"></a>// limitations under the License.
<a name="line14"></a>
<a name="line15"></a>/**
<a name="line16"></a> * @fileoverview Unit tests for goog.string.
<a name="line17"></a> */
<a name="line18"></a>
<a name="line19"></a>goog.provide(&#39;goog.stringTest&#39;);
<a name="line20"></a>
<a name="line21"></a>goog.require(&#39;goog.functions&#39;);
<a name="line22"></a>goog.require(&#39;goog.object&#39;);
<a name="line23"></a>goog.require(&#39;goog.string&#39;);
<a name="line24"></a>goog.require(&#39;goog.testing.PropertyReplacer&#39;);
<a name="line25"></a>goog.require(&#39;goog.testing.jsunit&#39;);
<a name="line26"></a>
<a name="line27"></a>goog.setTestOnly(&#39;goog.stringTest&#39;);
<a name="line28"></a>
<a name="line29"></a>var stubs = new goog.testing.PropertyReplacer();
<a name="line30"></a>
<a name="line31"></a>function tearDown() {
<a name="line32"></a>  stubs.reset();
<a name="line33"></a>}
<a name="line34"></a>
<a name="line35"></a>
<a name="line36"></a>//=== tests for goog.string.collapseWhitespace ===
<a name="line37"></a>
<a name="line38"></a>function testCollapseWhiteSpace() {
<a name="line39"></a>  var f = goog.string.collapseWhitespace;
<a name="line40"></a>
<a name="line41"></a>  assertEquals(&#39;Leading spaces not stripped&#39;, f(&#39;  abc&#39;), &#39;abc&#39;);
<a name="line42"></a>  assertEquals(&#39;Trailing spaces not stripped&#39;, f(&#39;abc  &#39;), &#39;abc&#39;);
<a name="line43"></a>  assertEquals(&#39;Wrapping spaces not stripped&#39;, f(&#39;  abc  &#39;), &#39;abc&#39;);
<a name="line44"></a>
<a name="line45"></a>  assertEquals(&#39;All white space chars not stripped&#39;,
<a name="line46"></a>               f(&#39;\xa0\n\t abc\xa0\n\t &#39;), &#39;abc&#39;);
<a name="line47"></a>
<a name="line48"></a>  assertEquals(&#39;Spaces not collapsed&#39;, f(&#39;a   b    c&#39;), &#39;a b c&#39;);
<a name="line49"></a>
<a name="line50"></a>  assertEquals(&#39;Tabs not collapsed&#39;, f(&#39;a\t\t\tb\tc&#39;), &#39;a b c&#39;);
<a name="line51"></a>
<a name="line52"></a>  assertEquals(&#39;All check failed&#39;, f(&#39; \ta \t \t\tb\t\n\xa0  c  \t\n&#39;),
<a name="line53"></a>               &#39;a b c&#39;);
<a name="line54"></a>}
<a name="line55"></a>
<a name="line56"></a>
<a name="line57"></a>//=== tests for goog.string.isEmpty ===
<a name="line58"></a>
<a name="line59"></a>function testIsEmpty() {
<a name="line60"></a>  assert(&#39;1. Should be empty&#39;, goog.string.isEmpty(&#39;&#39;));
<a name="line61"></a>  assert(&#39;2. Should be empty&#39;, goog.string.isEmpty(&#39; &#39;));
<a name="line62"></a>  assert(&#39;3. Should be empty&#39;, goog.string.isEmpty(&#39;    &#39;));
<a name="line63"></a>  assert(&#39;4. Should be empty&#39;, goog.string.isEmpty(&#39; \t\t\n\xa0   &#39;));
<a name="line64"></a>
<a name="line65"></a>  assert(&#39;1. Should not be empty&#39;, !goog.string.isEmpty(&#39; abc \t\xa0&#39;));
<a name="line66"></a>  assert(&#39;2. Should not be empty&#39;, !goog.string.isEmpty(&#39; a b c \t&#39;));
<a name="line67"></a>  assert(&#39;3. Should not be empty&#39;, !goog.string.isEmpty(&#39;;&#39;));
<a name="line68"></a>
<a name="line69"></a>
<a name="line70"></a>  var a;
<a name="line71"></a>  assert(&#39;Undefined is not empty&#39;, !goog.string.isEmpty(a));
<a name="line72"></a>  assert(&#39;Null is not empty&#39;, !goog.string.isEmpty(null));
<a name="line73"></a>  assert(&#39;A random object is not empty&#39;, !goog.string.isEmpty({a: 1, b: 2}));
<a name="line74"></a>}
<a name="line75"></a>
<a name="line76"></a>//=== tests for goog.string.isEmptySafe ===
<a name="line77"></a>
<a name="line78"></a>function testIsEmptySafe() {
<a name="line79"></a>  assert(&#39;1. Should be empty&#39;, goog.string.isEmptySafe(&#39;&#39;));
<a name="line80"></a>  assert(&#39;2. Should be empty&#39;, goog.string.isEmptySafe(&#39; &#39;));
<a name="line81"></a>  assert(&#39;3. Should be empty&#39;, goog.string.isEmptySafe(&#39;    &#39;));
<a name="line82"></a>  assert(&#39;4. Should be empty&#39;, goog.string.isEmptySafe(&#39; \t\t\n\xa0   &#39;));
<a name="line83"></a>
<a name="line84"></a>  assert(&#39;1. Should not be empty&#39;, !goog.string.isEmptySafe(&#39; abc \t\xa0&#39;));
<a name="line85"></a>  assert(&#39;2. Should not be empty&#39;, !goog.string.isEmptySafe(&#39; a b c \t&#39;));
<a name="line86"></a>  assert(&#39;3. Should not be empty&#39;, !goog.string.isEmptySafe(&#39;;&#39;));
<a name="line87"></a>
<a name="line88"></a>
<a name="line89"></a>  var a;
<a name="line90"></a>  assert(&#39;Undefined should be empty (safe)&#39;, goog.string.isEmptySafe(a));
<a name="line91"></a>  assert(&#39;Null should be empty (safe)&#39;, goog.string.isEmptySafe(null));
<a name="line92"></a>  assert(&#39;A random object is not empty&#39;,
<a name="line93"></a>         !goog.string.isEmptySafe({a: 1, b: 2}));
<a name="line94"></a>}
<a name="line95"></a>
<a name="line96"></a>//=== tests for goog.string.isAlpha ===
<a name="line97"></a>function testIsAlpha() {
<a name="line98"></a>  assertTrue(&#39;&quot;a&quot; should be alpha&#39;, goog.string.isAlpha(&#39;a&#39;));
<a name="line99"></a>  assertTrue(&#39;&quot;n&quot; should be alpha&#39;, goog.string.isAlpha(&#39;n&#39;));
<a name="line100"></a>  assertTrue(&#39;&quot;z&quot; should be alpha&#39;, goog.string.isAlpha(&#39;z&#39;));
<a name="line101"></a>  assertTrue(&#39;&quot;A&quot; should be alpha&#39;, goog.string.isAlpha(&#39;A&#39;));
<a name="line102"></a>  assertTrue(&#39;&quot;N&quot; should be alpha&#39;, goog.string.isAlpha(&#39;N&#39;));
<a name="line103"></a>  assertTrue(&#39;&quot;Z&quot; should be alpha&#39;, goog.string.isAlpha(&#39;Z&#39;));
<a name="line104"></a>  assertTrue(&#39;&quot;aa&quot; should be alpha&#39;, goog.string.isAlpha(&#39;aa&#39;));
<a name="line105"></a>  assertTrue(&#39;null is alpha&#39;, goog.string.isAlpha(null));
<a name="line106"></a>  assertTrue(&#39;undefined is alpha&#39;, goog.string.isAlpha(undefined));
<a name="line107"></a>
<a name="line108"></a>  assertFalse(&#39;&quot;aa!&quot; is not alpha&#39;, goog.string.isAlpha(&#39;aa!s&#39;));
<a name="line109"></a>  assertFalse(&#39;&quot;!&quot; is not alpha&#39;, goog.string.isAlpha(&#39;!&#39;));
<a name="line110"></a>  assertFalse(&#39;&quot;0&quot; is not alpha&#39;, goog.string.isAlpha(&#39;0&#39;));
<a name="line111"></a>  assertFalse(&#39;&quot;5&quot; is not alpha&#39;, goog.string.isAlpha(&#39;5&#39;));
<a name="line112"></a>}
<a name="line113"></a>
<a name="line114"></a>
<a name="line115"></a>
<a name="line116"></a>//=== tests for goog.string.isNumeric ===
<a name="line117"></a>function testIsNumeric() {
<a name="line118"></a>  assertTrue(&#39;&quot;8&quot; is a numeric string&#39;, goog.string.isNumeric(&#39;8&#39;));
<a name="line119"></a>  assertTrue(&#39;&quot;5&quot; is a numeric string&#39;, goog.string.isNumeric(&#39;5&#39;));
<a name="line120"></a>  assertTrue(&#39;&quot;34&quot; is a numeric string&#39;, goog.string.isNumeric(&#39;34&#39;));
<a name="line121"></a>  assertTrue(&#39;34 is a number&#39;, goog.string.isNumeric(34));
<a name="line122"></a>
<a name="line123"></a>  assertFalse(&#39;&quot;3.14&quot; has a period&#39;, goog.string.isNumeric(&#39;3.14&#39;));
<a name="line124"></a>  assertFalse(&#39;&quot;A&quot; is a letter&#39;, goog.string.isNumeric(&#39;A&#39;));
<a name="line125"></a>  assertFalse(&#39;&quot;!&quot; is punctuation&#39;, goog.string.isNumeric(&#39;!&#39;));
<a name="line126"></a>  assertFalse(&#39;null is not numeric&#39;, goog.string.isNumeric(null));
<a name="line127"></a>  assertFalse(&#39;undefined is not numeric&#39;, goog.string.isNumeric(undefined));
<a name="line128"></a>}
<a name="line129"></a>
<a name="line130"></a>
<a name="line131"></a>//=== tests for tests for goog.string.isAlphaNumeric ===
<a name="line132"></a>function testIsAlphaNumeric() {
<a name="line133"></a>  assertTrue(&#39;&quot;ABCabc&quot; should be alphanumeric&#39;,
<a name="line134"></a>             goog.string.isAlphaNumeric(&#39;ABCabc&#39;));
<a name="line135"></a>  assertTrue(&#39;&quot;123&quot; should be alphanumeric&#39;, goog.string.isAlphaNumeric(&#39;123&#39;));
<a name="line136"></a>  assertTrue(&#39;&quot;ABCabc123&quot; should be alphanumeric&#39;,
<a name="line137"></a>             goog.string.isAlphaNumeric(&#39;ABCabc123&#39;));
<a name="line138"></a>  assertTrue(&#39;null is alphanumeric&#39;, goog.string.isAlphaNumeric(null));
<a name="line139"></a>  assertTrue(&#39;undefined is alphanumeric&#39;,
<a name="line140"></a>             goog.string.isAlphaNumeric(undefined));
<a name="line141"></a>
<a name="line142"></a>  assertFalse(&#39;&quot;123!&quot; should not be alphanumeric&#39;,
<a name="line143"></a>              goog.string.isAlphaNumeric(&#39;123!&#39;));
<a name="line144"></a>  assertFalse(&#39;&quot;  &quot; should not be alphanumeric&#39;,
<a name="line145"></a>              goog.string.isAlphaNumeric(&#39;  &#39;));
<a name="line146"></a>}
<a name="line147"></a>
<a name="line148"></a>
<a name="line149"></a>//== tests for goog.string.isBreakingWhitespace ===
<a name="line150"></a>
<a name="line151"></a>function testIsBreakingWhitespace() {
<a name="line152"></a>  assertTrue(&#39;&quot; &quot; is breaking&#39;, goog.string.isBreakingWhitespace(&#39; &#39;));
<a name="line153"></a>  assertTrue(&#39;&quot;\\n&quot; is breaking&#39;, goog.string.isBreakingWhitespace(&#39;\n&#39;));
<a name="line154"></a>  assertTrue(&#39;&quot;\\t&quot; is breaking&#39;, goog.string.isBreakingWhitespace(&#39;\t&#39;));
<a name="line155"></a>  assertTrue(&#39;&quot;\\r&quot; is breaking&#39;, goog.string.isBreakingWhitespace(&#39;\r&#39;));
<a name="line156"></a>  assertTrue(&#39;&quot;\\r\\n\\t &quot; is breaking&#39;,
<a name="line157"></a>      goog.string.isBreakingWhitespace(&#39;\r\n\t &#39;));
<a name="line158"></a>
<a name="line159"></a>  assertFalse(&#39;nbsp is non-breaking&#39;, goog.string.isBreakingWhitespace(&#39;\xa0&#39;));
<a name="line160"></a>  assertFalse(&#39;&quot;a&quot; is non-breaking&#39;, goog.string.isBreakingWhitespace(&#39;a&#39;));
<a name="line161"></a>  assertFalse(&#39;&quot;a\\r&quot; is non-breaking&#39;,
<a name="line162"></a>      goog.string.isBreakingWhitespace(&#39;a\r&#39;));
<a name="line163"></a>}
<a name="line164"></a>
<a name="line165"></a>
<a name="line166"></a>//=== tests for goog.string.isSpace ===
<a name="line167"></a>function testIsSpace() {
<a name="line168"></a>  assertTrue(&#39;&quot; &quot; is a space&#39;, goog.string.isSpace(&#39; &#39;));
<a name="line169"></a>
<a name="line170"></a>  assertFalse(&#39;&quot;\\n&quot; is not a space&#39;, goog.string.isSpace(&#39;\n&#39;));
<a name="line171"></a>  assertFalse(&#39;&quot;\\t&quot; is not a space&#39;, goog.string.isSpace(&#39;\t&#39;));
<a name="line172"></a>  assertFalse(&#39;&quot;  &quot; is not a space, it\&#39;s two spaces&#39;,
<a name="line173"></a>              goog.string.isSpace(&#39;  &#39;));
<a name="line174"></a>  assertFalse(&#39;&quot;a&quot; is not a space&#39;, goog.string.isSpace(&#39;a&#39;));
<a name="line175"></a>  assertFalse(&#39;&quot;3&quot; is not a space&#39;, goog.string.isSpace(&#39;3&#39;));
<a name="line176"></a>  assertFalse(&#39;&quot;#&quot; is not a space&#39;, goog.string.isSpace(&#39;#&#39;));
<a name="line177"></a>  assertFalse(&#39;null is not a space&#39;, goog.string.isSpace(null));
<a name="line178"></a>  assertFalse(&#39;nbsp is not a space&#39;, goog.string.isSpace(&#39;\xa0&#39;));
<a name="line179"></a>}
<a name="line180"></a>
<a name="line181"></a>
<a name="line182"></a>// === tests for goog.string.stripNewlines ===
<a name="line183"></a>function testStripNewLines() {
<a name="line184"></a>  assertEquals(&#39;Should replace new lines with spaces&#39;,
<a name="line185"></a>               goog.string.stripNewlines(&#39;some\nlines\rthat\r\nare\n\nsplit&#39;),
<a name="line186"></a>               &#39;some lines that are split&#39;);
<a name="line187"></a>}
<a name="line188"></a>
<a name="line189"></a>
<a name="line190"></a>// === tests for goog.string.canonicalizeNewlines ===
<a name="line191"></a>function testCanonicalizeNewlines() {
<a name="line192"></a>  assertEquals(&#39;Should replace all types of new line with \\n&#39;,
<a name="line193"></a>               goog.string.canonicalizeNewlines(
<a name="line194"></a>                    &#39;some\nlines\rthat\r\nare\n\nsplit&#39;),
<a name="line195"></a>               &#39;some\nlines\nthat\nare\n\nsplit&#39;);
<a name="line196"></a>}
<a name="line197"></a>
<a name="line198"></a>
<a name="line199"></a>// === tests for goog.string.normalizeWhitespace ===
<a name="line200"></a>function testWhitespace() {
<a name="line201"></a>  assertEquals(&#39;All whitespace chars should be replaced with a normal space&#39;,
<a name="line202"></a>               goog.string.normalizeWhitespace(&#39;\xa0 \n\t \xa0 \n\t&#39;),
<a name="line203"></a>               &#39;         &#39;);
<a name="line204"></a>}
<a name="line205"></a>
<a name="line206"></a>
<a name="line207"></a>// === tests for goog.string.normalizeSpaces ===
<a name="line208"></a>function testNormalizeSpaces() {
<a name="line209"></a>  assertEquals(&#39;All whitespace chars should be replaced with a normal space&#39;,
<a name="line210"></a>               goog.string.normalizeSpaces(&#39;\xa0 \t \xa0 \t&#39;),
<a name="line211"></a>               &#39;    &#39;);
<a name="line212"></a>}
<a name="line213"></a>
<a name="line214"></a>function testCollapseBreakingSpaces() {
<a name="line215"></a>  assertEquals(&#39;breaking spaces are collapsed&#39;, &#39;a b&#39;,
<a name="line216"></a>      goog.string.collapseBreakingSpaces(&#39; \t\r\n a \t\r\n b \t\r\n &#39;));
<a name="line217"></a>  assertEquals(&#39;non-breaking spaces are kept&#39;, &#39;a \u00a0\u2000 b&#39;,
<a name="line218"></a>      goog.string.collapseBreakingSpaces(&#39;a \u00a0\u2000 b&#39;));
<a name="line219"></a>}
<a name="line220"></a>
<a name="line221"></a>/// === tests for goog.string.trim ===
<a name="line222"></a>function testTrim() {
<a name="line223"></a>  assertEquals(&#39;Should be the same&#39;, goog.string.trim(&#39;nothing 2 trim&#39;),
<a name="line224"></a>               &#39;nothing 2 trim&#39;);
<a name="line225"></a>  assertEquals(&#39;Remove spaces&#39;, goog.string.trim(&#39;   hello  goodbye   &#39;),
<a name="line226"></a>               &#39;hello  goodbye&#39;);
<a name="line227"></a>  assertEquals(&#39;Trim other stuff&#39;, goog.string.trim(&#39;\n\r\xa0 hi \r\n\xa0&#39;),
<a name="line228"></a>               &#39;hi&#39;);
<a name="line229"></a>}
<a name="line230"></a>
<a name="line231"></a>
<a name="line232"></a>/// === tests for goog.string.trimLeft ===
<a name="line233"></a>function testTrimLeft() {
<a name="line234"></a>  var f = goog.string.trimLeft;
<a name="line235"></a>  assertEquals(&#39;Should be the same&#39;, f(&#39;nothing to trim&#39;), &#39;nothing to trim&#39;);
<a name="line236"></a>  assertEquals(&#39;Remove spaces&#39;, f(&#39;   hello  goodbye   &#39;), &#39;hello  goodbye   &#39;);
<a name="line237"></a>  assertEquals(&#39;Trim other stuff&#39;, f(&#39;\xa0\n\r hi \r\n\xa0&#39;), &#39;hi \r\n\xa0&#39;);
<a name="line238"></a>}
<a name="line239"></a>
<a name="line240"></a>
<a name="line241"></a>/// === tests for goog.string.trimRight ===
<a name="line242"></a>function testTrimRight() {
<a name="line243"></a>  var f = goog.string.trimRight;
<a name="line244"></a>  assertEquals(&#39;Should be the same&#39;, f(&#39;nothing to trim&#39;), &#39;nothing to trim&#39;);
<a name="line245"></a>  assertEquals(&#39;Remove spaces&#39;, f(&#39;   hello  goodbye   &#39;), &#39;   hello  goodbye&#39;);
<a name="line246"></a>  assertEquals(&#39;Trim other stuff&#39;, f(&#39;\n\r\xa0 hi \r\n\xa0&#39;), &#39;\n\r\xa0 hi&#39;);
<a name="line247"></a>}
<a name="line248"></a>
<a name="line249"></a>
<a name="line250"></a>// === tests for goog.string.startsWith ===
<a name="line251"></a>function testStartsWith() {
<a name="line252"></a>  assertTrue(&#39;Should start with \&#39;\&#39;&#39;, goog.string.startsWith(&#39;abcd&#39;, &#39;&#39;));
<a name="line253"></a>  assertTrue(&#39;Should start with \&#39;ab\&#39;&#39;, goog.string.startsWith(&#39;abcd&#39;, &#39;ab&#39;));
<a name="line254"></a>  assertTrue(&#39;Should start with \&#39;abcd\&#39;&#39;,
<a name="line255"></a>             goog.string.startsWith(&#39;abcd&#39;, &#39;abcd&#39;));
<a name="line256"></a>  assertFalse(&#39;Should not start with \&#39;bcd\&#39;&#39;,
<a name="line257"></a>              goog.string.startsWith(&#39;abcd&#39;, &#39;bcd&#39;));
<a name="line258"></a>}
<a name="line259"></a>
<a name="line260"></a>function testEndsWith() {
<a name="line261"></a>  assertTrue(&#39;Should end with \&#39;\&#39;&#39;, goog.string.endsWith(&#39;abcd&#39;, &#39;&#39;));
<a name="line262"></a>  assertTrue(&#39;Should end with \&#39;ab\&#39;&#39;, goog.string.endsWith(&#39;abcd&#39;, &#39;cd&#39;));
<a name="line263"></a>  assertTrue(&#39;Should end with \&#39;abcd\&#39;&#39;, goog.string.endsWith(&#39;abcd&#39;, &#39;abcd&#39;));
<a name="line264"></a>  assertFalse(&#39;Should not end \&#39;abc\&#39;&#39;, goog.string.endsWith(&#39;abcd&#39;, &#39;abc&#39;));
<a name="line265"></a>  assertFalse(&#39;Should not end \&#39;abcde\&#39;&#39;,
<a name="line266"></a>              goog.string.endsWith(&#39;abcd&#39;, &#39;abcde&#39;));
<a name="line267"></a>}
<a name="line268"></a>
<a name="line269"></a>
<a name="line270"></a>// === tests for goog.string.caseInsensitiveStartsWith ===
<a name="line271"></a>function testCaseInsensitiveStartsWith() {
<a name="line272"></a>  assertTrue(&#39;Should start with \&#39;\&#39;&#39;,
<a name="line273"></a>             goog.string.caseInsensitiveStartsWith(&#39;abcd&#39;, &#39;&#39;));
<a name="line274"></a>  assertTrue(&#39;Should start with \&#39;ab\&#39;&#39;,
<a name="line275"></a>             goog.string.caseInsensitiveStartsWith(&#39;abcd&#39;, &#39;Ab&#39;));
<a name="line276"></a>  assertTrue(&#39;Should start with \&#39;abcd\&#39;&#39;,
<a name="line277"></a>             goog.string.caseInsensitiveStartsWith(&#39;AbCd&#39;, &#39;abCd&#39;));
<a name="line278"></a>  assertFalse(&#39;Should not start with \&#39;bcd\&#39;&#39;,
<a name="line279"></a>              goog.string.caseInsensitiveStartsWith(&#39;ABCD&#39;, &#39;bcd&#39;));
<a name="line280"></a>}
<a name="line281"></a>
<a name="line282"></a>// === tests for goog.string.caseInsensitiveEndsWith ===
<a name="line283"></a>function testCaseInsensitiveEndsWith() {
<a name="line284"></a>  assertTrue(&#39;Should end with \&#39;\&#39;&#39;,
<a name="line285"></a>             goog.string.caseInsensitiveEndsWith(&#39;abcd&#39;, &#39;&#39;));
<a name="line286"></a>  assertTrue(&#39;Should end with \&#39;cd\&#39;&#39;,
<a name="line287"></a>             goog.string.caseInsensitiveEndsWith(&#39;abCD&#39;, &#39;cd&#39;));
<a name="line288"></a>  assertTrue(&#39;Should end with \&#39;abcd\&#39;&#39;,
<a name="line289"></a>             goog.string.caseInsensitiveEndsWith(&#39;abcd&#39;, &#39;abCd&#39;));
<a name="line290"></a>  assertFalse(&#39;Should not end \&#39;abc\&#39;&#39;,
<a name="line291"></a>              goog.string.caseInsensitiveEndsWith(&#39;aBCd&#39;, &#39;ABc&#39;));
<a name="line292"></a>  assertFalse(&#39;Should not end \&#39;abcde\&#39;&#39;,
<a name="line293"></a>              goog.string.caseInsensitiveEndsWith(&#39;ABCD&#39;, &#39;abcde&#39;));
<a name="line294"></a>}
<a name="line295"></a>
<a name="line296"></a>
<a name="line297"></a>// === tests for goog.string.subs ===
<a name="line298"></a>function testSubs() {
<a name="line299"></a>  assertEquals(&#39;Should be the same&#39;, goog.string.subs(&#39;nothing to subs&#39;),
<a name="line300"></a>               &#39;nothing to subs&#39;);
<a name="line301"></a>  assertEquals(&#39;Should be the same&#39;, goog.string.subs(&#39;%s&#39;, &#39;1&#39;), &#39;1&#39;);
<a name="line302"></a>  assertEquals(&#39;Should be the same&#39;,
<a name="line303"></a>               goog.string.subs(&#39;%s%s%s&#39;, &#39;1&#39;, 2, true), &#39;12true&#39;);
<a name="line304"></a>  function f() {
<a name="line305"></a>    assertTrue(&#39;This should not be called&#39;, false);
<a name="line306"></a>  }
<a name="line307"></a>  f.toString = function() { return &#39;f&#39;; };
<a name="line308"></a>  assertEquals(&#39;Should not call function&#39;, goog.string.subs(&#39;%s&#39;, f), &#39;f&#39;);
<a name="line309"></a>
<a name="line310"></a>  // If the string that is to be substituted in contains $&amp; then it will be
<a name="line311"></a>  // usually be replaced with %s, we need to check goog.string.subs, handles
<a name="line312"></a>  // this case.
<a name="line313"></a>  assertEquals(&#39;$&amp; should not be substituted with %s&#39;, &#39;Foo Bar $&amp;&#39;,
<a name="line314"></a>      goog.string.subs(&#39;Foo %s&#39;, &#39;Bar $&amp;&#39;));
<a name="line315"></a>
<a name="line316"></a>  assertEquals(&#39;$$ should not be substituted&#39;, &#39;_$$_&#39;,
<a name="line317"></a>               goog.string.subs(&#39;%s&#39;, &#39;_$$_&#39;));
<a name="line318"></a>  assertEquals(&#39;$` should not be substituted&#39;, &#39;_$`_&#39;,
<a name="line319"></a>               goog.string.subs(&#39;%s&#39;, &#39;_$`_&#39;));
<a name="line320"></a>  assertEquals(&#39;$\&#39; should not be substituted&#39;, &#39;_$\&#39;_&#39;,
<a name="line321"></a>               goog.string.subs(&#39;%s&#39;, &#39;_$\&#39;_&#39;));
<a name="line322"></a>  for (var i = 0; i &lt; 99; i += 9) {
<a name="line323"></a>    assertEquals(&#39;$&#39; + i + &#39; should not be substituted&#39;,
<a name="line324"></a>        &#39;_$&#39; + i + &#39;_&#39;, goog.string.subs(&#39;%s&#39;, &#39;_$&#39; + i + &#39;_&#39;));
<a name="line325"></a>  }
<a name="line326"></a>}
<a name="line327"></a>
<a name="line328"></a>
<a name="line329"></a>// === tests for goog.string.caseInsensitiveCompare ===
<a name="line330"></a>function testCaseInsensitiveCompare() {
<a name="line331"></a>  var f = goog.string.caseInsensitiveCompare;
<a name="line332"></a>
<a name="line333"></a>  assert(&#39;&quot;ABC&quot; should be less than &quot;def&quot;&#39;, f(&#39;ABC&#39;, &#39;def&#39;) == -1);
<a name="line334"></a>  assert(&#39;&quot;abc&quot; should be less than &quot;DEF&quot;&#39;, f(&#39;abc&#39;, &#39;DEF&#39;) == -1);
<a name="line335"></a>
<a name="line336"></a>  assert(&#39;&quot;XYZ&quot; should equal &quot;xyz&quot;&#39;, f(&#39;XYZ&#39;, &#39;xyz&#39;) == 0);
<a name="line337"></a>
<a name="line338"></a>  assert(&#39;&quot;XYZ&quot; should be greater than &quot;UVW&quot;&#39;, f(&#39;xyz&#39;, &#39;UVW&#39;) == 1);
<a name="line339"></a>  assert(&#39;&quot;XYZ&quot; should be greater than &quot;uvw&quot;&#39;, f(&#39;XYZ&#39;, &#39;uvw&#39;) == 1);
<a name="line340"></a>}
<a name="line341"></a>
<a name="line342"></a>
<a name="line343"></a>// === tests for goog.string.numerateCompare ===
<a name="line344"></a>function testNumerateCompare() {
<a name="line345"></a>  var f = goog.string.numerateCompare;
<a name="line346"></a>
<a name="line347"></a>  // Each comparison in this list is tested to assure that t[0] &lt; t[1],
<a name="line348"></a>  // t[1] &gt; t[0], and identity tests t[0] == t[0] and t[1] == t[1].
<a name="line349"></a>  var comparisons = [
<a name="line350"></a>    [&#39;&#39;, &#39;0&#39;],
<a name="line351"></a>    [&#39;2&#39;, &#39;10&#39;],
<a name="line352"></a>    [&#39;05&#39;, &#39;9&#39;],
<a name="line353"></a>    [&#39;3.14&#39;, &#39;3.2&#39;],
<a name="line354"></a>    [&#39;sub&#39;, &#39;substring&#39;],
<a name="line355"></a>    [&#39;Photo 7&#39;, &#39;photo 8&#39;], // Case insensitive for most sorts.
<a name="line356"></a>    [&#39;Mango&#39;, &#39;mango&#39;], // Case sensitive if strings are otherwise identical.
<a name="line357"></a>    [&#39;album 2 photo 20&#39;, &#39;album 10 photo 20&#39;],
<a name="line358"></a>    [&#39;album 7 photo 20&#39;, &#39;album 7 photo 100&#39;]];
<a name="line359"></a>
<a name="line360"></a>  for (var i = 0; i &lt; comparisons.length; i++) {
<a name="line361"></a>    var t = comparisons[i];
<a name="line362"></a>    assert(t[0] + &#39; should be less than &#39; + t[1],
<a name="line363"></a>           f(t[0], t[1]) &lt; 0);
<a name="line364"></a>    assert(t[1] + &#39; should be greater than &#39; + t[0],
<a name="line365"></a>           f(t[1], t[0]) &gt; 0);
<a name="line366"></a>    assert(t[0] + &#39; should be equal to &#39; + t[0],
<a name="line367"></a>           f(t[0], t[0]) == 0);
<a name="line368"></a>    assert(t[1] + &#39; should be equal to &#39; + t[1],
<a name="line369"></a>           f(t[1], t[1]) == 0);
<a name="line370"></a>  }
<a name="line371"></a>}
<a name="line372"></a>
<a name="line373"></a>// === tests for goog.string.urlEncode &amp;&amp; .urlDecode ===
<a name="line374"></a>// NOTE: When test was written it was simply an alias for the built in
<a name="line375"></a>// &#39;encodeURICompoent&#39;, therefore this test is simply used to make sure that in
<a name="line376"></a>// the future it doesn&#39;t get broken.
<a name="line377"></a>function testUrlEncodeAndDecode() {
<a name="line378"></a>  var input = &#39;&lt;p&gt;&quot;hello there,&quot; she said, &quot;what is going on here?&lt;/p&gt;&#39;;
<a name="line379"></a>  var output = &#39;%3Cp%3E%22hello%20there%2C%22%20she%20said%2C%20%22what%20is&#39; +
<a name="line380"></a>               &#39;%20going%20on%20here%3F%3C%2Fp%3E&#39;;
<a name="line381"></a>
<a name="line382"></a>  assertEquals(&#39;urlEncode vs encodeURIComponent&#39;,
<a name="line383"></a>               encodeURIComponent(input),
<a name="line384"></a>               goog.string.urlEncode(input));
<a name="line385"></a>
<a name="line386"></a>  assertEquals(&#39;urlEncode vs model&#39;, goog.string.urlEncode(input), output);
<a name="line387"></a>
<a name="line388"></a>  assertEquals(&#39;urlDecode vs model&#39;, goog.string.urlDecode(output), input);
<a name="line389"></a>
<a name="line390"></a>  assertEquals(&#39;urlDecode vs urlEncode&#39;,
<a name="line391"></a>               goog.string.urlDecode(goog.string.urlEncode(input)), input);
<a name="line392"></a>
<a name="line393"></a>  assertEquals(&#39;urlDecode with +s instead of %20s&#39;,
<a name="line394"></a>               goog.string.urlDecode(output.replace(/%20/g, &#39;+&#39;)),
<a name="line395"></a>               input);
<a name="line396"></a>}
<a name="line397"></a>
<a name="line398"></a>
<a name="line399"></a>// === tests for goog.string.newLineToBr ===
<a name="line400"></a>function testNewLineToBr() {
<a name="line401"></a>  var str = &#39;some\nlines\rthat\r\nare\n\nsplit&#39;;
<a name="line402"></a>  var html = &#39;some&lt;br&gt;lines&lt;br&gt;that&lt;br&gt;are&lt;br&gt;&lt;br&gt;split&#39;;
<a name="line403"></a>  var xhtml = &#39;some&lt;br /&gt;lines&lt;br /&gt;that&lt;br /&gt;are&lt;br /&gt;&lt;br /&gt;split&#39;;
<a name="line404"></a>
<a name="line405"></a>  assertEquals(&#39;Should be html&#39;, goog.string.newLineToBr(str), html);
<a name="line406"></a>  assertEquals(&#39;Should be html&#39;, goog.string.newLineToBr(str, false), html);
<a name="line407"></a>  assertEquals(&#39;Should be xhtml&#39;, goog.string.newLineToBr(str, true), xhtml);
<a name="line408"></a>
<a name="line409"></a>}
<a name="line410"></a>
<a name="line411"></a>
<a name="line412"></a>// === tests for goog.string.htmlEscape and .unescapeEntities ===
<a name="line413"></a>function testHtmlEscapeAndUnescapeEntities() {
<a name="line414"></a>  var text = &#39;&quot;x1 &lt; x2 &amp;&amp; y2 &gt; y1&quot;&#39;;
<a name="line415"></a>  var html = &#39;&amp;quot;x1 &amp;lt; x2 &amp;amp;&amp;amp; y2 &amp;gt; y1&amp;quot;&#39;;
<a name="line416"></a>
<a name="line417"></a>  assertEquals(&#39;Testing htmlEscape&#39;, goog.string.htmlEscape(text), html);
<a name="line418"></a>  assertEquals(&#39;Testing htmlEscape&#39;, goog.string.htmlEscape(text, false), html);
<a name="line419"></a>  assertEquals(&#39;Testing htmlEscape&#39;, goog.string.htmlEscape(text, true), html);
<a name="line420"></a>  assertEquals(&#39;Testing unescapeEntities&#39;,
<a name="line421"></a>               goog.string.unescapeEntities(html), text);
<a name="line422"></a>
<a name="line423"></a>  assertEquals(&#39;escape -&gt; unescape&#39;,
<a name="line424"></a>               goog.string.unescapeEntities(goog.string.htmlEscape(text)),
<a name="line425"></a>               text);
<a name="line426"></a>  assertEquals(&#39;unescape -&gt; escape&#39;,
<a name="line427"></a>               goog.string.htmlEscape(goog.string.unescapeEntities(html)),
<a name="line428"></a>               html);
<a name="line429"></a>}
<a name="line430"></a>
<a name="line431"></a>function testHtmlEscapeAndUnescapeEntitiesUsingDom() {
<a name="line432"></a>  var text = &#39;&quot;x1 &lt; x2 &amp;&amp; y2 &gt; y1&quot;&#39;;
<a name="line433"></a>  var html = &#39;&amp;quot;x1 &amp;lt; x2 &amp;amp;&amp;amp; y2 &amp;gt; y1&amp;quot;&#39;;
<a name="line434"></a>
<a name="line435"></a>  assertEquals(&#39;Testing unescapeEntities&#39;,
<a name="line436"></a>               goog.string.unescapeEntitiesUsingDom_(html), text);
<a name="line437"></a>  assertEquals(&#39;escape -&gt; unescape&#39;,
<a name="line438"></a>               goog.string.unescapeEntitiesUsingDom_(
<a name="line439"></a>                   goog.string.htmlEscape(text)), text);
<a name="line440"></a>  assertEquals(&#39;unescape -&gt; escape&#39;,
<a name="line441"></a>               goog.string.htmlEscape(
<a name="line442"></a>                   goog.string.unescapeEntitiesUsingDom_(html)), html);
<a name="line443"></a>}
<a name="line444"></a>
<a name="line445"></a>function testHtmlUnescapeEntitiesUsingDom_withAmpersands() {
<a name="line446"></a>  var html = &#39;&amp;lt;a&amp;b&amp;gt;&#39;;
<a name="line447"></a>  var text = &#39;&lt;a&amp;b&gt;&#39;;
<a name="line448"></a>
<a name="line449"></a>  assertEquals(&#39;wrong unescaped value&#39;,
<a name="line450"></a>      text, goog.string.unescapeEntitiesUsingDom_(html));
<a name="line451"></a>}
<a name="line452"></a>
<a name="line453"></a>function testHtmlEscapeAndUnescapePureXmlEntities_() {
<a name="line454"></a>  var text = &#39;&quot;x1 &lt; x2 &amp;&amp; y2 &gt; y1&quot;&#39;;
<a name="line455"></a>  var html = &#39;&amp;quot;x1 &amp;lt; x2 &amp;amp;&amp;amp; y2 &amp;gt; y1&amp;quot;&#39;;
<a name="line456"></a>
<a name="line457"></a>  assertEquals(&#39;Testing unescapePureXmlEntities_&#39;,
<a name="line458"></a>               goog.string.unescapePureXmlEntities_(html), text);
<a name="line459"></a>  assertEquals(&#39;escape -&gt; unescape&#39;,
<a name="line460"></a>               goog.string.unescapePureXmlEntities_(
<a name="line461"></a>                   goog.string.htmlEscape(text)), text);
<a name="line462"></a>  assertEquals(&#39;unescape -&gt; escape&#39;,
<a name="line463"></a>               goog.string.htmlEscape(
<a name="line464"></a>                   goog.string.unescapePureXmlEntities_(html)), html);
<a name="line465"></a>}
<a name="line466"></a>
<a name="line467"></a>var globalXssVar = 0;
<a name="line468"></a>
<a name="line469"></a>function testXssUnescapeEntities() {
<a name="line470"></a>  // This tests that we don&#39;t have any XSS exploits in unescapeEntities
<a name="line471"></a>  var test = &#39;&amp;amp;&lt;script defer&gt;globalXssVar=1;&lt;/&#39; + &#39;script&gt;&#39;;
<a name="line472"></a>  var expected = &#39;&amp;&lt;script defer&gt;globalXssVar=1;&lt;/&#39; + &#39;script&gt;&#39;;
<a name="line473"></a>
<a name="line474"></a>  assertEquals(&#39;Testing unescapeEntities&#39;, expected,
<a name="line475"></a>               goog.string.unescapeEntities(test));
<a name="line476"></a>  assertEquals(&#39;unescapeEntities is vulnarable to XSS&#39;, 0, globalXssVar);
<a name="line477"></a>
<a name="line478"></a>  test = &#39;&amp;amp;&lt;script&gt;globalXssVar=1;&lt;/&#39; + &#39;script&gt;&#39;;
<a name="line479"></a>  expected = &#39;&amp;&lt;script&gt;globalXssVar=1;&lt;/&#39; + &#39;script&gt;&#39;;
<a name="line480"></a>
<a name="line481"></a>  assertEquals(&#39;Testing unescapeEntities&#39;, expected,
<a name="line482"></a>               goog.string.unescapeEntities(test));
<a name="line483"></a>  assertEquals(&#39;unescapeEntities is vulnarable to XSS&#39;, 0, globalXssVar);
<a name="line484"></a>}
<a name="line485"></a>
<a name="line486"></a>
<a name="line487"></a>function testXssUnescapeEntitiesUsingDom() {
<a name="line488"></a>  // This tests that we don&#39;t have any XSS exploits in unescapeEntitiesUsingDom
<a name="line489"></a>  var test = &#39;&amp;amp;&lt;script defer&gt;globalXssVar=1;&lt;/&#39; + &#39;script&gt;&#39;;
<a name="line490"></a>  var expected = &#39;&amp;&lt;script defer&gt;globalXssVar=1;&lt;/&#39; + &#39;script&gt;&#39;;
<a name="line491"></a>
<a name="line492"></a>  assertEquals(&#39;Testing unescapeEntitiesUsingDom_&#39;, expected,
<a name="line493"></a>               goog.string.unescapeEntitiesUsingDom_(test));
<a name="line494"></a>  assertEquals(&#39;unescapeEntitiesUsingDom_ is vulnerable to XSS&#39;, 0,
<a name="line495"></a>               globalXssVar);
<a name="line496"></a>
<a name="line497"></a>  test = &#39;&amp;amp;&lt;script&gt;globalXssVar=1;&lt;/&#39; + &#39;script&gt;&#39;;
<a name="line498"></a>  expected = &#39;&amp;&lt;script&gt;globalXssVar=1;&lt;/&#39; + &#39;script&gt;&#39;;
<a name="line499"></a>
<a name="line500"></a>  assertEquals(&#39;Testing unescapeEntitiesUsingDom_&#39;, expected,
<a name="line501"></a>               goog.string.unescapeEntitiesUsingDom_(test));
<a name="line502"></a>  assertEquals(&#39;unescapeEntitiesUsingDom_ is vulnerable to XSS&#39;, 0,
<a name="line503"></a>               globalXssVar);
<a name="line504"></a>}
<a name="line505"></a>
<a name="line506"></a>
<a name="line507"></a>function testXssUnescapePureXmlEntities() {
<a name="line508"></a>  // This tests that we don&#39;t have any XSS exploits in unescapePureXmlEntities
<a name="line509"></a>  var test = &#39;&amp;amp;&lt;script defer&gt;globalXssVar=1;&lt;/&#39; + &#39;script&gt;&#39;;
<a name="line510"></a>  var expected = &#39;&amp;&lt;script defer&gt;globalXssVar=1;&lt;/&#39; + &#39;script&gt;&#39;;
<a name="line511"></a>
<a name="line512"></a>  assertEquals(&#39;Testing unescapePureXmlEntities_&#39;, expected,
<a name="line513"></a>               goog.string.unescapePureXmlEntities_(test));
<a name="line514"></a>  assertEquals(&#39;unescapePureXmlEntities_ is vulnarable to XSS&#39;, 0,
<a name="line515"></a>               globalXssVar);
<a name="line516"></a>
<a name="line517"></a>  test = &#39;&amp;amp;&lt;script&gt;globalXssVar=1;&lt;/&#39; + &#39;script&gt;&#39;;
<a name="line518"></a>  expected = &#39;&amp;&lt;script&gt;globalXssVar=1;&lt;/&#39; + &#39;script&gt;&#39;;
<a name="line519"></a>
<a name="line520"></a>  assertEquals(&#39;Testing unescapePureXmlEntities_&#39;, expected,
<a name="line521"></a>               goog.string.unescapePureXmlEntities_(test));
<a name="line522"></a>  assertEquals(&#39;unescapePureXmlEntities_ is vulnarable to XSS&#39;, 0,
<a name="line523"></a>               globalXssVar);
<a name="line524"></a>}
<a name="line525"></a>
<a name="line526"></a>
<a name="line527"></a>function testUnescapeEntitiesPreservesWhitespace() {
<a name="line528"></a>  // This tests that whitespace is preserved (primarily for IE)
<a name="line529"></a>  // Also make sure leading and trailing whitespace are preserved.
<a name="line530"></a>  var test = &#39;\nTesting\n\twhitespace\n    preservation\n&#39;;
<a name="line531"></a>  var expected = test;
<a name="line532"></a>
<a name="line533"></a>  assertEquals(&#39;Testing unescapeEntities&#39;, expected,
<a name="line534"></a>               goog.string.unescapeEntities(test));
<a name="line535"></a>
<a name="line536"></a>  // Now with entities
<a name="line537"></a>  test += &#39; &amp;amp;&amp;nbsp;\n&#39;;
<a name="line538"></a>  expected += &#39; &amp;\u00A0\n&#39;;
<a name="line539"></a>  assertEquals(&#39;Testing unescapeEntities&#39;, expected,
<a name="line540"></a>               goog.string.unescapeEntities(test));
<a name="line541"></a>}
<a name="line542"></a>
<a name="line543"></a>
<a name="line544"></a>// === tests for goog.string.whitespaceEscape ===
<a name="line545"></a>function testWhiteSpaceEscape() {
<a name="line546"></a>  assertEquals(&#39;Should be the same&#39;,
<a name="line547"></a>      goog.string.whitespaceEscape(&#39;one two  three   four    five     &#39;),
<a name="line548"></a>      &#39;one two &amp;#160;three &amp;#160; four &amp;#160; &amp;#160;five &amp;#160; &amp;#160; &#39;);
<a name="line549"></a>}
<a name="line550"></a>
<a name="line551"></a>
<a name="line552"></a>
<a name="line553"></a>// === tests for goog.string.stripQuotes ===
<a name="line554"></a>function testStripQuotes() {
<a name="line555"></a>  assertEquals(&#39;Quotes should be stripped&#39;,
<a name="line556"></a>               goog.string.stripQuotes(&#39;&quot;hello&quot;&#39;, &#39;&quot;&#39;),
<a name="line557"></a>               &#39;hello&#39;);
<a name="line558"></a>
<a name="line559"></a>  assertEquals(&#39;Quotes should be stripped&#39;,
<a name="line560"></a>               goog.string.stripQuotes(&#39;\&#39;hello\&#39;&#39;, &#39;\&#39;&#39;),
<a name="line561"></a>               &#39;hello&#39;);
<a name="line562"></a>
<a name="line563"></a>  assertEquals(&#39;Quotes should not be stripped&#39;,
<a name="line564"></a>               goog.string.stripQuotes(&#39;-&quot;hello&quot;&#39;, &#39;&quot;&#39;),
<a name="line565"></a>               &#39;-&quot;hello&quot;&#39;);
<a name="line566"></a>}
<a name="line567"></a>
<a name="line568"></a>function testStripQuotesMultiple() {
<a name="line569"></a>  assertEquals(&#39;Quotes should be stripped&#39;,
<a name="line570"></a>               goog.string.stripQuotes(&#39;&quot;hello&quot;&#39;, &#39;&quot;\&#39;&#39;),
<a name="line571"></a>               &#39;hello&#39;);
<a name="line572"></a>  assertEquals(&#39;Quotes should be stripped&#39;,
<a name="line573"></a>               goog.string.stripQuotes(&#39;\&#39;hello\&#39;&#39;, &#39;&quot;\&#39;&#39;),
<a name="line574"></a>               &#39;hello&#39;);
<a name="line575"></a>
<a name="line576"></a>  assertEquals(&#39;Quotes should be stripped&#39;,
<a name="line577"></a>               goog.string.stripQuotes(&#39;\&#39;hello\&#39;&#39;, &#39;&#39;),
<a name="line578"></a>               &#39;\&#39;hello\&#39;&#39;);
<a name="line579"></a>}
<a name="line580"></a>
<a name="line581"></a>function testStripQuotesMultiple2() {
<a name="line582"></a>  // Makes sure we do not strip twice
<a name="line583"></a>  assertEquals(&#39;Quotes should be stripped&#39;,
<a name="line584"></a>               goog.string.stripQuotes(&#39;&quot;\&#39;hello\&#39;&quot;&#39;, &#39;&quot;\&#39;&#39;),
<a name="line585"></a>               &#39;\&#39;hello\&#39;&#39;);
<a name="line586"></a>  assertEquals(&#39;Quotes should be stripped&#39;,
<a name="line587"></a>               goog.string.stripQuotes(&#39;&quot;\&#39;hello\&#39;&quot;&#39;, &#39;\&#39;&quot;&#39;),
<a name="line588"></a>               &#39;\&#39;hello\&#39;&#39;);
<a name="line589"></a>
<a name="line590"></a>}
<a name="line591"></a>
<a name="line592"></a>// === tests for goog.string.truncate ===
<a name="line593"></a>function testTruncate() {
<a name="line594"></a>  var str = &#39;abcdefghijklmnopqrstuvwxyz&#39;;
<a name="line595"></a>  assertEquals(&#39;Should be equal&#39;, goog.string.truncate(str, 8), &#39;abcde...&#39;);
<a name="line596"></a>  assertEquals(&#39;Should be equal&#39;, goog.string.truncate(str, 11), &#39;abcdefgh...&#39;);
<a name="line597"></a>
<a name="line598"></a>  var html = &#39;true &amp;amp;&amp;amp; false == false&#39;;
<a name="line599"></a>  assertEquals(&#39;Should clip html char&#39;, goog.string.truncate(html, 11),
<a name="line600"></a>               &#39;true &amp;am...&#39;);
<a name="line601"></a>  assertEquals(&#39;Should not clip html char&#39;,
<a name="line602"></a>               goog.string.truncate(html, 12, true),
<a name="line603"></a>               &#39;true &amp;amp;&amp;amp; f...&#39;);
<a name="line604"></a>}
<a name="line605"></a>
<a name="line606"></a>
<a name="line607"></a>// === tests for goog.string.truncateMiddle ===
<a name="line608"></a>function testTruncateMiddle() {
<a name="line609"></a>  var str = &#39;abcdefghijklmnopqrstuvwxyz&#39;;
<a name="line610"></a>  assertEquals(&#39;abc...xyz&#39;, goog.string.truncateMiddle(str, 6));
<a name="line611"></a>  assertEquals(&#39;abc...yz&#39;, goog.string.truncateMiddle(str, 5));
<a name="line612"></a>  assertEquals(str, goog.string.truncateMiddle(str, str.length));
<a name="line613"></a>
<a name="line614"></a>  var html = &#39;true &amp;amp;&amp;amp; false == false&#39;;
<a name="line615"></a>  assertEquals(&#39;Should clip html char&#39;, &#39;true &amp;a...= false&#39;,
<a name="line616"></a>      goog.string.truncateMiddle(html, 14));
<a name="line617"></a>  assertEquals(&#39;Should not clip html char&#39;,
<a name="line618"></a>      &#39;true &amp;amp;&amp;amp;...= false&#39;,
<a name="line619"></a>       goog.string.truncateMiddle(html, 14, true));
<a name="line620"></a>
<a name="line621"></a>  assertEquals(&#39;ab...xyz&#39;, goog.string.truncateMiddle(str, 5, null, 3));
<a name="line622"></a>  assertEquals(&#39;abcdefg...xyz&#39;, goog.string.truncateMiddle(str, 10, null, 3));
<a name="line623"></a>  assertEquals(&#39;abcdef...wxyz&#39;, goog.string.truncateMiddle(str, 10, null, 4));
<a name="line624"></a>  assertEquals(&#39;...yz&#39;, goog.string.truncateMiddle(str, 2, null, 3));
<a name="line625"></a>  assertEquals(str, goog.string.truncateMiddle(str, 50, null, 3));
<a name="line626"></a>
<a name="line627"></a>  assertEquals(&#39;Should clip html char&#39;, &#39;true &amp;amp;&amp;...lse&#39;,
<a name="line628"></a>      goog.string.truncateMiddle(html, 14, null, 3));
<a name="line629"></a>  assertEquals(&#39;Should not clip html char&#39;,
<a name="line630"></a>      &#39;true &amp;amp;&amp;amp; fal...lse&#39;,
<a name="line631"></a>       goog.string.truncateMiddle(html, 14, true, 3));
<a name="line632"></a>}
<a name="line633"></a>
<a name="line634"></a>
<a name="line635"></a>// === goog.string.quote ===
<a name="line636"></a>function testQuote() {
<a name="line637"></a>  var str = allChars();
<a name="line638"></a>  assertEquals(str, eval(goog.string.quote(str)));
<a name="line639"></a>
<a name="line640"></a>  // empty string
<a name="line641"></a>  assertEquals(&#39;&#39;, eval(goog.string.quote(&#39;&#39;)));
<a name="line642"></a>
<a name="line643"></a>  // unicode
<a name="line644"></a>  str = allChars(0, 10000);
<a name="line645"></a>  assertEquals(str, eval(goog.string.quote(str)));
<a name="line646"></a>}
<a name="line647"></a>
<a name="line648"></a>function testQuoteSpecialChars() {
<a name="line649"></a>  assertEquals(&#39;&quot;\\&quot;&quot;&#39;, goog.string.quote(&#39;&quot;&#39;));
<a name="line650"></a>  assertEquals(&#39;&quot;\&#39;&quot;&#39;, goog.string.quote(&quot;&#39;&quot;));
<a name="line651"></a>  assertEquals(&#39;&quot;\\\\&quot;&#39;, goog.string.quote(&#39;\\&#39;));
<a name="line652"></a>
<a name="line653"></a>  var zeroQuoted = goog.string.quote(&#39;\0&#39;);
<a name="line654"></a>  assertTrue(
<a name="line655"></a>      &#39;goog.string.quote mangles the 0 char: &#39;,
<a name="line656"></a>      &#39;&quot;\\0&quot;&#39; == zeroQuoted || &#39;&quot;\\x00&quot;&#39; == zeroQuoted);
<a name="line657"></a>}
<a name="line658"></a>
<a name="line659"></a>function testCrossBrowserQuote() {
<a name="line660"></a>  // The vertical space char has weird semantics on jscript, so we don&#39;t test
<a name="line661"></a>  // that one.
<a name="line662"></a>  var vertChar = &#39;\x0B&#39;.charCodeAt(0);
<a name="line663"></a>
<a name="line664"></a>  // The zero char has two alternate encodings (\0 and \x00) both are ok,
<a name="line665"></a>  // and tested above.
<a name="line666"></a>  var zeroChar = 0;
<a name="line667"></a>
<a name="line668"></a>  var str = allChars(zeroChar + 1, vertChar) + allChars(vertChar + 1, 10000);
<a name="line669"></a>  var nativeQuote = goog.string.quote(str);
<a name="line670"></a>
<a name="line671"></a>  stubs.set(String.prototype, &#39;quote&#39;, null);
<a name="line672"></a>  assertNull(&#39;&#39;.quote);
<a name="line673"></a>
<a name="line674"></a>  assertEquals(nativeQuote, goog.string.quote(str));
<a name="line675"></a>}
<a name="line676"></a>
<a name="line677"></a>function allChars(opt_start, opt_end) {
<a name="line678"></a>  opt_start = opt_start || 0;
<a name="line679"></a>  opt_end = opt_end || 256;
<a name="line680"></a>  var rv = &#39;&#39;;
<a name="line681"></a>  for (var i = opt_start; i &lt; opt_end; i++) {
<a name="line682"></a>    rv += String.fromCharCode(i);
<a name="line683"></a>  }
<a name="line684"></a>  return rv;
<a name="line685"></a>}
<a name="line686"></a>
<a name="line687"></a>function testEscapeString() {
<a name="line688"></a>  var expected = allChars(0, 10000);
<a name="line689"></a>  try {
<a name="line690"></a>    var actual =
<a name="line691"></a>        eval(&#39;&quot;&#39; + goog.string.escapeString(expected) + &#39;&quot;&#39;);
<a name="line692"></a>  } catch (e) {
<a name="line693"></a>    fail(&#39;Quote failed: err &#39; + e.message);
<a name="line694"></a>  }
<a name="line695"></a>  assertEquals(expected, actual);
<a name="line696"></a>}
<a name="line697"></a>
<a name="line698"></a>function testCountOf() {
<a name="line699"></a>  assertEquals(goog.string.countOf(&#39;REDSOXROX&#39;, undefined), 0);
<a name="line700"></a>  assertEquals(goog.string.countOf(&#39;REDSOXROX&#39;, null), 0);
<a name="line701"></a>  assertEquals(goog.string.countOf(&#39;REDSOXROX&#39;, &#39;&#39;), 0);
<a name="line702"></a>  assertEquals(goog.string.countOf(&#39;&#39;, undefined), 0);
<a name="line703"></a>  assertEquals(goog.string.countOf(&#39;&#39;, null), 0);
<a name="line704"></a>  assertEquals(goog.string.countOf(&#39;&#39;, &#39;&#39;), 0);
<a name="line705"></a>  assertEquals(goog.string.countOf(&#39;&#39;, &#39;REDSOXROX&#39;), 0);
<a name="line706"></a>  assertEquals(goog.string.countOf(undefined, &#39;R&#39;), 0);
<a name="line707"></a>  assertEquals(goog.string.countOf(null, &#39;R&#39;), 0);
<a name="line708"></a>  assertEquals(goog.string.countOf(undefined, undefined), 0);
<a name="line709"></a>  assertEquals(goog.string.countOf(null, null), 0);
<a name="line710"></a>
<a name="line711"></a>  assertEquals(goog.string.countOf(&#39;REDSOXROX&#39;, &#39;R&#39;), 2);
<a name="line712"></a>  assertEquals(goog.string.countOf(&#39;REDSOXROX&#39;, &#39;E&#39;), 1);
<a name="line713"></a>  assertEquals(goog.string.countOf(&#39;REDSOXROX&#39;, &#39;X&#39;), 2);
<a name="line714"></a>  assertEquals(goog.string.countOf(&#39;REDSOXROX&#39;, &#39;RED&#39;), 1);
<a name="line715"></a>  assertEquals(goog.string.countOf(&#39;REDSOXROX&#39;, &#39;ROX&#39;), 1);
<a name="line716"></a>  assertEquals(goog.string.countOf(&#39;REDSOXROX&#39;, &#39;OX&#39;), 2);
<a name="line717"></a>  assertEquals(goog.string.countOf(&#39;REDSOXROX&#39;, &#39;Z&#39;), 0);
<a name="line718"></a>  assertEquals(goog.string.countOf(&#39;REDSOXROX&#39;, &#39;REDSOXROX&#39;), 1);
<a name="line719"></a>  assertEquals(goog.string.countOf(&#39;REDSOXROX&#39;, &#39;YANKEES&#39;), 0);
<a name="line720"></a>  assertEquals(goog.string.countOf(&#39;REDSOXROX&#39;, &#39;EVIL_EMPIRE&#39;), 0);
<a name="line721"></a>
<a name="line722"></a>  assertEquals(goog.string.countOf(&#39;RRRRRRRRR&#39;, &#39;R&#39;), 9);
<a name="line723"></a>  assertEquals(goog.string.countOf(&#39;RRRRRRRRR&#39;, &#39;RR&#39;), 4);
<a name="line724"></a>  assertEquals(goog.string.countOf(&#39;RRRRRRRRR&#39;, &#39;RRR&#39;), 3);
<a name="line725"></a>  assertEquals(goog.string.countOf(&#39;RRRRRRRRR&#39;, &#39;RRRR&#39;), 2);
<a name="line726"></a>  assertEquals(goog.string.countOf(&#39;RRRRRRRRR&#39;, &#39;RRRRR&#39;), 1);
<a name="line727"></a>  assertEquals(goog.string.countOf(&#39;RRRRRRRRR&#39;, &#39;RRRRRR&#39;), 1);
<a name="line728"></a>}
<a name="line729"></a>
<a name="line730"></a>function testRemoveAt() {
<a name="line731"></a>  var str = &#39;barfoobarbazbar&#39;;
<a name="line732"></a>  str = goog.string.removeAt(str, 0, 3);
<a name="line733"></a>  assertEquals(&#39;Remove first bar&#39;, &#39;foobarbazbar&#39;, str);
<a name="line734"></a>  str = goog.string.removeAt(str, 3, 3);
<a name="line735"></a>  assertEquals(&#39;Remove middle bar&#39;, &#39;foobazbar&#39;, str);
<a name="line736"></a>  str = goog.string.removeAt(str, 6, 3);
<a name="line737"></a>  assertEquals(&#39;Remove last bar&#39;, &#39;foobaz&#39;, str);
<a name="line738"></a>  assertEquals(&#39;Invalid negative index&#39;, &#39;foobaz&#39;,
<a name="line739"></a>      goog.string.removeAt(str, -1, 0));
<a name="line740"></a>  assertEquals(&#39;Invalid overflow index&#39;, &#39;foobaz&#39;,
<a name="line741"></a>      goog.string.removeAt(str, 9, 0));
<a name="line742"></a>  assertEquals(&#39;Invalid negative stringLength&#39;, &#39;foobaz&#39;,
<a name="line743"></a>      goog.string.removeAt(str, 0, -1));
<a name="line744"></a>  assertEquals(&#39;Invalid overflow stringLength&#39;, &#39;&#39;,
<a name="line745"></a>      goog.string.removeAt(str, 0, 9));
<a name="line746"></a>  assertEquals(&#39;Invalid overflow index and stringLength&#39;, &#39;foobaz&#39;,
<a name="line747"></a>      goog.string.removeAt(str, 9, 9));
<a name="line748"></a>  assertEquals(&#39;Invalid zero stringLength&#39;, &#39;foobaz&#39;,
<a name="line749"></a>      goog.string.removeAt(str, 0, 0));
<a name="line750"></a>}
<a name="line751"></a>
<a name="line752"></a>function testRemove() {
<a name="line753"></a>  var str = &#39;barfoobarbazbar&#39;;
<a name="line754"></a>  str = goog.string.remove(str, &#39;bar&#39;);
<a name="line755"></a>  assertEquals(&#39;Remove first bar&#39;, &#39;foobarbazbar&#39;, str);
<a name="line756"></a>  str = goog.string.remove(str, &#39;bar&#39;);
<a name="line757"></a>  assertEquals(&#39;Remove middle bar&#39;, &#39;foobazbar&#39;, str);
<a name="line758"></a>  str = goog.string.remove(str, &#39;bar&#39;);
<a name="line759"></a>  assertEquals(&#39;Remove last bar&#39;, &#39;foobaz&#39;, str);
<a name="line760"></a>  str = goog.string.remove(str, &#39;bar&#39;);
<a name="line761"></a>  assertEquals(&#39;Original string&#39;, &#39;foobaz&#39;, str);
<a name="line762"></a>}
<a name="line763"></a>
<a name="line764"></a>function testRemoveAll() {
<a name="line765"></a>  var str = &#39;foobarbazbarfoobazfoo&#39;;
<a name="line766"></a>  str = goog.string.removeAll(str, &#39;foo&#39;);
<a name="line767"></a>  assertEquals(&#39;Remove all occurrences of foo&#39;, &#39;barbazbarbaz&#39;, str);
<a name="line768"></a>  str = goog.string.removeAll(str, &#39;foo&#39;);
<a name="line769"></a>  assertEquals(&#39;Original string&#39;, &#39;barbazbarbaz&#39;, str);
<a name="line770"></a>}
<a name="line771"></a>
<a name="line772"></a>function testRegExpEscape() {
<a name="line773"></a>  var spec = &#39;()[]{}+-?*.$^|,:#&lt;!\\&#39;;
<a name="line774"></a>  var escapedSpec = &#39;\\&#39; + spec.split(&#39;&#39;).join(&#39;\\&#39;);
<a name="line775"></a>  assertEquals(&#39;special chars&#39;, escapedSpec, goog.string.regExpEscape(spec));
<a name="line776"></a>  assertEquals(&#39;backslash b&#39;, &#39;\\x08&#39;, goog.string.regExpEscape(&#39;\b&#39;));
<a name="line777"></a>
<a name="line778"></a>  var s = allChars();
<a name="line779"></a>  var re = new RegExp(&#39;^&#39; + goog.string.regExpEscape(s) + &#39;$&#39;);
<a name="line780"></a>  assertTrue(&#39;All ASCII&#39;, re.test(s));
<a name="line781"></a>  s = &#39;&#39;;
<a name="line782"></a>  var re = new RegExp(&#39;^&#39; + goog.string.regExpEscape(s) + &#39;$&#39;);
<a name="line783"></a>  assertTrue(&#39;empty string&#39;, re.test(s));
<a name="line784"></a>  s = allChars(0, 10000);
<a name="line785"></a>  var re = new RegExp(&#39;^&#39; + goog.string.regExpEscape(s) + &#39;$&#39;);
<a name="line786"></a>  assertTrue(&#39;Unicode&#39;, re.test(s));
<a name="line787"></a>}
<a name="line788"></a>
<a name="line789"></a>function testPadNumber() {
<a name="line790"></a>  assertEquals(&#39;01.250&#39;, goog.string.padNumber(1.25, 2, 3));
<a name="line791"></a>  assertEquals(&#39;01.25&#39;, goog.string.padNumber(1.25, 2));
<a name="line792"></a>  assertEquals(&#39;01.3&#39;, goog.string.padNumber(1.25, 2, 1));
<a name="line793"></a>  assertEquals(&#39;1.25&#39;, goog.string.padNumber(1.25, 0));
<a name="line794"></a>  assertEquals(&#39;10&#39;, goog.string.padNumber(9.9, 2, 0));
<a name="line795"></a>  assertEquals(&#39;7&#39;, goog.string.padNumber(7, 0));
<a name="line796"></a>  assertEquals(&#39;7&#39;, goog.string.padNumber(7, 1));
<a name="line797"></a>  assertEquals(&#39;07&#39;, goog.string.padNumber(7, 2));
<a name="line798"></a>}
<a name="line799"></a>
<a name="line800"></a>function testAsString() {
<a name="line801"></a>  assertEquals(&#39;&#39;, goog.string.makeSafe(null));
<a name="line802"></a>  assertEquals(&#39;&#39;, goog.string.makeSafe(undefined));
<a name="line803"></a>  assertEquals(&#39;&#39;, goog.string.makeSafe(&#39;&#39;));
<a name="line804"></a>
<a name="line805"></a>  assertEquals(&#39;abc&#39;, goog.string.makeSafe(&#39;abc&#39;));
<a name="line806"></a>  assertEquals(&#39;123&#39;, goog.string.makeSafe(123));
<a name="line807"></a>  assertEquals(&#39;0&#39;, goog.string.makeSafe(0));
<a name="line808"></a>
<a name="line809"></a>  assertEquals(&#39;true&#39;, goog.string.makeSafe(true));
<a name="line810"></a>  assertEquals(&#39;false&#39;, goog.string.makeSafe(false));
<a name="line811"></a>
<a name="line812"></a>  var funky = function() {};
<a name="line813"></a>  funky.toString = function() { return &#39;funky-thing&#39; };
<a name="line814"></a>  assertEquals(&#39;funky-thing&#39;, goog.string.makeSafe(funky));
<a name="line815"></a>}
<a name="line816"></a>
<a name="line817"></a>function testStringRepeat() {
<a name="line818"></a>  assertEquals(&#39;&#39;, goog.string.repeat(&#39;*&#39;, 0));
<a name="line819"></a>  assertEquals(&#39;*&#39;, goog.string.repeat(&#39;*&#39;, 1));
<a name="line820"></a>  assertEquals(&#39;     &#39;, goog.string.repeat(&#39; &#39;, 5));
<a name="line821"></a>  assertEquals(&#39;__________&#39;, goog.string.repeat(&#39;_&#39;, 10));
<a name="line822"></a>  assertEquals(&#39;aaa&#39;, goog.string.repeat(&#39;a&#39;, 3));
<a name="line823"></a>  assertEquals(&#39;foofoofoofoofoofoo&#39;, goog.string.repeat(&#39;foo&#39;, 6));
<a name="line824"></a>}
<a name="line825"></a>
<a name="line826"></a>function testBuildString() {
<a name="line827"></a>  assertEquals(&#39;&#39;, goog.string.buildString());
<a name="line828"></a>  assertEquals(&#39;a&#39;, goog.string.buildString(&#39;a&#39;));
<a name="line829"></a>  assertEquals(&#39;ab&#39;, goog.string.buildString(&#39;ab&#39;));
<a name="line830"></a>  assertEquals(&#39;ab&#39;, goog.string.buildString(&#39;a&#39;, &#39;b&#39;));
<a name="line831"></a>  assertEquals(&#39;abcd&#39;, goog.string.buildString(&#39;a&#39;, &#39;b&#39;, &#39;c&#39;, &#39;d&#39;));
<a name="line832"></a>  assertEquals(&#39;0&#39;, goog.string.buildString(0));
<a name="line833"></a>  assertEquals(&#39;0123&#39;, goog.string.buildString(0, 1, 2, 3));
<a name="line834"></a>  assertEquals(&#39;ab01&#39;, goog.string.buildString(&#39;a&#39;, &#39;b&#39;, 0, 1));
<a name="line835"></a>  assertEquals(&#39;&#39;, goog.string.buildString(null, undefined));
<a name="line836"></a>}
<a name="line837"></a>
<a name="line838"></a>function testCompareVersions() {
<a name="line839"></a>  var f = goog.string.compareVersions;
<a name="line840"></a>  assertTrue(&#39;numeric equality broken&#39;, f(1, 1) == 0);
<a name="line841"></a>  assertTrue(&#39;numeric less than broken&#39;, f(1.0, 1.1) &lt; 0);
<a name="line842"></a>  assertTrue(&#39;numeric greater than broken&#39;, f(2.0, 1.1) &gt; 0);
<a name="line843"></a>
<a name="line844"></a>  assertTrue(&#39;exact equality broken&#39;, f(&#39;1.0&#39;, &#39;1.0&#39;) == 0);
<a name="line845"></a>  assertTrue(&#39;mutlidot equality broken&#39;, f(&#39;1.0.0.0&#39;, &#39;1.0&#39;) == 0);
<a name="line846"></a>  assertTrue(&#39;mutlidigit equality broken&#39;, f(&#39;1.000&#39;, &#39;1.0&#39;) == 0);
<a name="line847"></a>  assertTrue(&#39;less than broken&#39;, f(&#39;1.0.2.1&#39;, &#39;1.1&#39;) &lt; 0);
<a name="line848"></a>  assertTrue(&#39;greater than broken&#39;, f(&#39;1.1&#39;, &#39;1.0.2.1&#39;) &gt; 0);
<a name="line849"></a>
<a name="line850"></a>  assertTrue(&#39;substring less than broken&#39;, f(&#39;1&#39;, &#39;1.1&#39;) &lt; 0);
<a name="line851"></a>  assertTrue(&#39;substring greater than broken&#39;, f(&#39;2.2&#39;, &#39;2&#39;) &gt; 0);
<a name="line852"></a>
<a name="line853"></a>  assertTrue(&#39;b greater than broken&#39;, f(&#39;1.1&#39;, &#39;1.1b&#39;) &gt; 0);
<a name="line854"></a>  assertTrue(&#39;b less than broken&#39;, f(&#39;1.1b&#39;, &#39;1.1&#39;) &lt; 0);
<a name="line855"></a>  assertTrue(&#39;b equality broken&#39;, f(&#39;1.1b&#39;, &#39;1.1b&#39;) == 0);
<a name="line856"></a>
<a name="line857"></a>  assertTrue(&#39;b &gt; a broken&#39;, f(&#39;1.1b&#39;, &#39;1.1a&#39;) &gt; 0);
<a name="line858"></a>  assertTrue(&#39;a &lt; b broken&#39;, f(&#39;1.1a&#39;, &#39;1.1b&#39;) &lt; 0);
<a name="line859"></a>
<a name="line860"></a>  assertTrue(&#39;9.5 &lt; 9.10 broken&#39;, f(&#39;9.5&#39;, &#39;9.10&#39;) &lt; 0);
<a name="line861"></a>  assertTrue(&#39;9.5 &lt; 9.11 broken&#39;, f(&#39;9.5&#39;, &#39;9.11&#39;) &lt; 0);
<a name="line862"></a>  assertTrue(&#39;9.11 &gt; 9.10 broken&#39;, f(&#39;9.11&#39;, &#39;9.10&#39;) &gt; 0);
<a name="line863"></a>  assertTrue(&#39;9.1 &lt; 9.10 broken&#39;, f(&#39;9.1&#39;, &#39;9.10&#39;) &lt; 0);
<a name="line864"></a>  assertTrue(&#39;9.1.1 &lt; 9.10 broken&#39;, f(&#39;9.1.1&#39;, &#39;9.10&#39;) &lt; 0);
<a name="line865"></a>  assertTrue(&#39;9.1.1 &lt; 9.11 broken&#39;, f(&#39;9.1.1&#39;, &#39;9.11&#39;) &lt; 0);
<a name="line866"></a>
<a name="line867"></a>  assertTrue(&#39;10a &gt; 9b broken&#39;, f(&#39;1.10a&#39;, &#39;1.9b&#39;) &gt; 0);
<a name="line868"></a>  assertTrue(&#39;b &lt; b2 broken&#39;, f(&#39;1.1b&#39;, &#39;1.1b2&#39;) &lt; 0);
<a name="line869"></a>  assertTrue(&#39;b10 &gt; b9 broken&#39;, f(&#39;1.1b10&#39;, &#39;1.1b9&#39;) &gt; 0);
<a name="line870"></a>
<a name="line871"></a>  assertTrue(&#39;7 &gt; 6 broken with leading whitespace&#39;, f(&#39; 7&#39;, &#39;6&#39;) &gt; 0);
<a name="line872"></a>  assertTrue(&#39;7 &gt; 6 broken with trailing whitespace&#39;, f(&#39;7 &#39;, &#39;6&#39;) &gt; 0);
<a name="line873"></a>}
<a name="line874"></a>
<a name="line875"></a>function testIsUnicodeChar() {
<a name="line876"></a>  assertFalse(&#39;empty string broken&#39;, goog.string.isUnicodeChar(&#39;&#39;));
<a name="line877"></a>  assertFalse(&#39;non-single char string broken&#39;,
<a name="line878"></a>              goog.string.isUnicodeChar(&#39;abc&#39;));
<a name="line879"></a>  assertTrue(&#39;space broken&#39;, goog.string.isUnicodeChar(&#39; &#39;));
<a name="line880"></a>  assertTrue(&#39;single char broken&#39;, goog.string.isUnicodeChar(&#39;a&#39;));
<a name="line881"></a>  assertTrue(&#39;upper case broken&#39;, goog.string.isUnicodeChar(&#39;A&#39;));
<a name="line882"></a>  assertTrue(&#39;unicode char broken&#39;, goog.string.isUnicodeChar(&#39;\u0C07&#39;));
<a name="line883"></a>}
<a name="line884"></a>
<a name="line885"></a>function assertHashcodeEquals(expectedHashCode, str) {
<a name="line886"></a>  assertEquals(&#39;wrong hashCode for &#39; + str.substring(0, 32),
<a name="line887"></a>      expectedHashCode, goog.string.hashCode(str));
<a name="line888"></a>}
<a name="line889"></a>
<a name="line890"></a>/**
<a name="line891"></a> * Verify we get random-ish looking values for hash of Strings.
<a name="line892"></a> */
<a name="line893"></a>function testHashCode() {
<a name="line894"></a>  try {
<a name="line895"></a>    goog.string.hashCode(null);
<a name="line896"></a>    fail(&#39;should throw exception for null&#39;);
<a name="line897"></a>  } catch (ex) {
<a name="line898"></a>    // success
<a name="line899"></a>  }
<a name="line900"></a>  assertHashcodeEquals(0, &#39;&#39;);
<a name="line901"></a>  assertHashcodeEquals(101574, &#39;foo&#39;);
<a name="line902"></a>  assertHashcodeEquals(1301670364, &#39;\uAAAAfoo&#39;);
<a name="line903"></a>  assertHashcodeEquals(92567585, goog.string.repeat(&#39;a&#39;, 5));
<a name="line904"></a>  assertHashcodeEquals(2869595232, goog.string.repeat(&#39;a&#39;, 6));
<a name="line905"></a>  assertHashcodeEquals(3058106369, goog.string.repeat(&#39;a&#39;, 7));
<a name="line906"></a>  assertHashcodeEquals(312017024, goog.string.repeat(&#39;a&#39;, 8));
<a name="line907"></a>  assertHashcodeEquals(2929737728, goog.string.repeat(&#39;a&#39;, 1024));
<a name="line908"></a>}
<a name="line909"></a>
<a name="line910"></a>function testUniqueString() {
<a name="line911"></a>  var TEST_COUNT = 20;
<a name="line912"></a>
<a name="line913"></a>  var obj = {};
<a name="line914"></a>  for (var i = 0; i &lt; TEST_COUNT; i++) {
<a name="line915"></a>    obj[goog.string.createUniqueString()] = true;
<a name="line916"></a>  }
<a name="line917"></a>
<a name="line918"></a>  assertEquals(&#39;All strings should be unique.&#39;, TEST_COUNT,
<a name="line919"></a>      goog.object.getCount(obj));
<a name="line920"></a>}
<a name="line921"></a>
<a name="line922"></a>function testToNumber() {
<a name="line923"></a>  // First, test the cases goog.string.toNumber() was primarily written for,
<a name="line924"></a>  // because JS built-ins are dumb.
<a name="line925"></a>  assertNaN(goog.string.toNumber(&#39;123a&#39;));
<a name="line926"></a>  assertNaN(goog.string.toNumber(&#39;123.456.78&#39;));
<a name="line927"></a>  assertNaN(goog.string.toNumber(&#39;&#39;));
<a name="line928"></a>  assertNaN(goog.string.toNumber(&#39; &#39;));
<a name="line929"></a>
<a name="line930"></a>  // Now, sanity-check.
<a name="line931"></a>  assertEquals(123, goog.string.toNumber(&#39; 123 &#39;));
<a name="line932"></a>  assertEquals(321.123, goog.string.toNumber(&#39;321.123&#39;));
<a name="line933"></a>  assertEquals(1.00001, goog.string.toNumber(&#39;1.00001&#39;));
<a name="line934"></a>  assertEquals(1, goog.string.toNumber(&#39;1.00000&#39;));
<a name="line935"></a>  assertEquals(0.2, goog.string.toNumber(&#39;0.20&#39;));
<a name="line936"></a>  assertEquals(0, goog.string.toNumber(&#39;0&#39;));
<a name="line937"></a>  assertEquals(0, goog.string.toNumber(&#39;0.0&#39;));
<a name="line938"></a>  assertEquals(-1, goog.string.toNumber(&#39;-1&#39;));
<a name="line939"></a>  assertEquals(-0.3, goog.string.toNumber(&#39;-.3&#39;));
<a name="line940"></a>  assertEquals(-12.345, goog.string.toNumber(&#39;-12.345&#39;));
<a name="line941"></a>  assertEquals(100, goog.string.toNumber(&#39;1e2&#39;));
<a name="line942"></a>  assertEquals(0.123, goog.string.toNumber(&#39;12.3e-2&#39;));
<a name="line943"></a>  assertNaN(goog.string.toNumber(&#39;abc&#39;));
<a name="line944"></a>}
<a name="line945"></a>
<a name="line946"></a>function testGetRandomString() {
<a name="line947"></a>  stubs.set(goog, &#39;now&#39;, goog.functions.constant(1295726605874));
<a name="line948"></a>  stubs.set(Math, &#39;random&#39;, goog.functions.constant(0.6679361383522245));
<a name="line949"></a>  assertTrue(&#39;String must be alphanumeric&#39;,
<a name="line950"></a>             goog.string.isAlphaNumeric(goog.string.getRandomString()));
<a name="line951"></a>}
<a name="line952"></a>
<a name="line953"></a>function testToCamelCase() {
<a name="line954"></a>  assertEquals(&#39;OneTwoThree&#39;, goog.string.toCamelCase(&#39;-one-two-three&#39;));
<a name="line955"></a>  assertEquals(&#39;oneTwoThree&#39;, goog.string.toCamelCase(&#39;one-two-three&#39;));
<a name="line956"></a>  assertEquals(&#39;oneTwo&#39;, goog.string.toCamelCase(&#39;one-two&#39;));
<a name="line957"></a>  assertEquals(&#39;one&#39;, goog.string.toCamelCase(&#39;one&#39;));
<a name="line958"></a>  assertEquals(&#39;oneTwo&#39;, goog.string.toCamelCase(&#39;oneTwo&#39;));
<a name="line959"></a>  assertEquals(&#39;String value matching a native function name.&#39;,
<a name="line960"></a>      &#39;toString&#39;, goog.string.toCamelCase(&#39;toString&#39;));
<a name="line961"></a>}
<a name="line962"></a>
<a name="line963"></a>function testToSelectorCase() {
<a name="line964"></a>  assertEquals(&#39;-one-two-three&#39;, goog.string.toSelectorCase(&#39;OneTwoThree&#39;));
<a name="line965"></a>  assertEquals(&#39;one-two-three&#39;, goog.string.toSelectorCase(&#39;oneTwoThree&#39;));
<a name="line966"></a>  assertEquals(&#39;one-two&#39;, goog.string.toSelectorCase(&#39;oneTwo&#39;));
<a name="line967"></a>  assertEquals(&#39;one&#39;, goog.string.toSelectorCase(&#39;one&#39;));
<a name="line968"></a>  assertEquals(&#39;one-two&#39;, goog.string.toSelectorCase(&#39;one-two&#39;));
<a name="line969"></a>  assertEquals(&#39;String value matching a native function name.&#39;,
<a name="line970"></a>      &#39;to-string&#39;, goog.string.toSelectorCase(&#39;toString&#39;));
<a name="line971"></a>}
<a name="line972"></a>
<a name="line973"></a>function testToTitleCase() {
<a name="line974"></a>  assertEquals(&#39;One&#39;, goog.string.toTitleCase(&#39;one&#39;));
<a name="line975"></a>  assertEquals(&#39;CamelCase&#39;, goog.string.toTitleCase(&#39;camelCase&#39;));
<a name="line976"></a>  assertEquals(&#39;Onelongword&#39;, goog.string.toTitleCase(&#39;onelongword&#39;));
<a name="line977"></a>  assertEquals(&#39;One Two Three&#39;, goog.string.toTitleCase(&#39;one two three&#39;));
<a name="line978"></a>  assertEquals(&#39;One        Two    Three&#39;,
<a name="line979"></a>      goog.string.toTitleCase(&#39;one        two    three&#39;));
<a name="line980"></a>  assertEquals(&#39;   Longword  &#39;, goog.string.toTitleCase(&#39;   longword  &#39;));
<a name="line981"></a>  assertEquals(&#39;One-two-three&#39;, goog.string.toTitleCase(&#39;one-two-three&#39;));
<a name="line982"></a>  assertEquals(&#39;One_two_three&#39;, goog.string.toTitleCase(&#39;one_two_three&#39;));
<a name="line983"></a>  assertEquals(&#39;String value matching a native function name.&#39;,
<a name="line984"></a>      &#39;ToString&#39;, goog.string.toTitleCase(&#39;toString&#39;));
<a name="line985"></a>
<a name="line986"></a>  // Verify results with no delimiter.
<a name="line987"></a>  assertEquals(&#39;One two three&#39;, goog.string.toTitleCase(&#39;one two three&#39;, &#39;&#39;));
<a name="line988"></a>  assertEquals(&#39;One-two-three&#39;, goog.string.toTitleCase(&#39;one-two-three&#39;, &#39;&#39;));
<a name="line989"></a>  assertEquals(&#39; onetwothree&#39;, goog.string.toTitleCase(&#39; onetwothree&#39;, &#39;&#39;));
<a name="line990"></a>
<a name="line991"></a>  // Verify results with one delimiter.
<a name="line992"></a>  assertEquals(&#39;One two&#39;, goog.string.toTitleCase(&#39;one two&#39;, &#39;.&#39;));
<a name="line993"></a>  assertEquals(&#39; one two&#39;, goog.string.toTitleCase(&#39; one two&#39;, &#39;.&#39;));
<a name="line994"></a>  assertEquals(&#39; one.Two&#39;, goog.string.toTitleCase(&#39; one.two&#39;, &#39;.&#39;));
<a name="line995"></a>  assertEquals(&#39;One.Two&#39;, goog.string.toTitleCase(&#39;one.two&#39;, &#39;.&#39;));
<a name="line996"></a>  assertEquals(&#39;One...Two...&#39;, goog.string.toTitleCase(&#39;one...two...&#39;, &#39;.&#39;));
<a name="line997"></a>
<a name="line998"></a>  // Verify results with multiple delimiters.
<a name="line999"></a>  var delimiters = &#39;_-.&#39;;
<a name="line1000"></a>  assertEquals(&#39;One two three&#39;,
<a name="line1001"></a>      goog.string.toTitleCase(&#39;one two three&#39;, delimiters));
<a name="line1002"></a>  assertEquals(&#39;  one two three&#39;,
<a name="line1003"></a>      goog.string.toTitleCase(&#39;  one two three&#39;, delimiters));
<a name="line1004"></a>  assertEquals(&#39;One-Two-Three&#39;,
<a name="line1005"></a>      goog.string.toTitleCase(&#39;one-two-three&#39;, delimiters));
<a name="line1006"></a>  assertEquals(&#39;One_Two_Three&#39;,
<a name="line1007"></a>      goog.string.toTitleCase(&#39;one_two_three&#39;, delimiters));
<a name="line1008"></a>  assertEquals(&#39;One...Two...Three&#39;,
<a name="line1009"></a>      goog.string.toTitleCase(&#39;one...two...three&#39;, delimiters));
<a name="line1010"></a>  assertEquals(&#39;One.  two.  three&#39;,
<a name="line1011"></a>      goog.string.toTitleCase(&#39;one.  two.  three&#39;, delimiters));
<a name="line1012"></a>}
<a name="line1013"></a>
<a name="line1014"></a>function testParseInt() {
<a name="line1015"></a>  // Many example values borrowed from
<a name="line1016"></a>  // http://trac.webkit.org/browser/trunk/LayoutTests/fast/js/kde/
<a name="line1017"></a>  // GlobalObject-expected.txt
<a name="line1018"></a>
<a name="line1019"></a>  // Check non-numbers and strings
<a name="line1020"></a>  assertTrue(isNaN(goog.string.parseInt(undefined)));
<a name="line1021"></a>  assertTrue(isNaN(goog.string.parseInt(null)));
<a name="line1022"></a>  assertTrue(isNaN(goog.string.parseInt({})));
<a name="line1023"></a>
<a name="line1024"></a>  assertTrue(isNaN(goog.string.parseInt(&#39;&#39;)));
<a name="line1025"></a>  assertTrue(isNaN(goog.string.parseInt(&#39; &#39;)));
<a name="line1026"></a>  assertTrue(isNaN(goog.string.parseInt(&#39;a&#39;)));
<a name="line1027"></a>  assertTrue(isNaN(goog.string.parseInt(&#39;FFAA&#39;)));
<a name="line1028"></a>  assertEquals(1, goog.string.parseInt(1))
<a name="line1029"></a>  assertEquals(1234567890123456, goog.string.parseInt(1234567890123456))
<a name="line1030"></a>  assertEquals(2, goog.string.parseInt(&#39; 2.3&#39;));
<a name="line1031"></a>  assertEquals(16, goog.string.parseInt(&#39;0x10&#39;));
<a name="line1032"></a>  assertEquals(11, goog.string.parseInt(&#39;11&#39;));
<a name="line1033"></a>  assertEquals(15, goog.string.parseInt(&#39;0xF&#39;));
<a name="line1034"></a>  assertEquals(15, goog.string.parseInt(&#39;0XF&#39;));
<a name="line1035"></a>  assertEquals(3735928559, goog.string.parseInt(&#39;0XDEADBEEF&#39;));
<a name="line1036"></a>  assertEquals(3, goog.string.parseInt(&#39;3x&#39;));
<a name="line1037"></a>  assertEquals(3, goog.string.parseInt(&#39;3 x&#39;));
<a name="line1038"></a>  assertFalse(isFinite(goog.string.parseInt(&#39;Infinity&#39;)));
<a name="line1039"></a>  assertEquals(15, goog.string.parseInt(&#39;15&#39;));
<a name="line1040"></a>  assertEquals(15, goog.string.parseInt(&#39;015&#39;));
<a name="line1041"></a>  assertEquals(15, goog.string.parseInt(&#39;0xf&#39;));
<a name="line1042"></a>  assertEquals(15, goog.string.parseInt(&#39;15&#39;));
<a name="line1043"></a>  assertEquals(15, goog.string.parseInt(&#39;0xF&#39;));
<a name="line1044"></a>  assertEquals(15, goog.string.parseInt(&#39;15.99&#39;));
<a name="line1045"></a>  assertTrue(isNaN(goog.string.parseInt(&#39;FXX123&#39;)));
<a name="line1046"></a>  assertEquals(15, goog.string.parseInt(&#39;15*3&#39;));
<a name="line1047"></a>  assertEquals(7, goog.string.parseInt(&#39;0x7&#39;));
<a name="line1048"></a>  assertEquals(1, goog.string.parseInt(&#39;1x7&#39;));
<a name="line1049"></a>
<a name="line1050"></a>  // Strings have no special meaning
<a name="line1051"></a>  assertTrue(isNaN(goog.string.parseInt(&#39;Infinity&#39;)));
<a name="line1052"></a>  assertTrue(isNaN(goog.string.parseInt(&#39;NaN&#39;)));
<a name="line1053"></a>
<a name="line1054"></a>  // Test numbers and values
<a name="line1055"></a>  assertEquals(3, goog.string.parseInt(3.3));
<a name="line1056"></a>  assertEquals(-3, goog.string.parseInt(-3.3));
<a name="line1057"></a>  assertEquals(0, goog.string.parseInt(-0));
<a name="line1058"></a>  assertTrue(isNaN(goog.string.parseInt(Infinity)));
<a name="line1059"></a>  assertTrue(isNaN(goog.string.parseInt(NaN)));
<a name="line1060"></a>  assertTrue(isNaN(goog.string.parseInt(Number.POSITIVE_INFINITY)));
<a name="line1061"></a>  assertTrue(isNaN(goog.string.parseInt(Number.NEGATIVE_INFINITY)));
<a name="line1062"></a>
<a name="line1063"></a>  // In Chrome (at least), parseInt(Number.MIN_VALUE) is 5 (5e-324) and
<a name="line1064"></a>  // parseInt(Number.MAX_VALUE) is 1 (1.79...e+308) as they are converted
<a name="line1065"></a>  // to strings.  We do not attempt to correct this behavior.
<a name="line1066"></a>
<a name="line1067"></a>  // Additional values for negatives.
<a name="line1068"></a>  assertEquals(-3, goog.string.parseInt(&#39;-3&#39;));
<a name="line1069"></a>  assertEquals(-32, goog.string.parseInt(&#39;-32    &#39;));
<a name="line1070"></a>  assertEquals(-32, goog.string.parseInt(&#39; -32 &#39;));
<a name="line1071"></a>  assertEquals(-3, goog.string.parseInt(&#39;-0x3&#39;));
<a name="line1072"></a>  assertEquals(-50, goog.string.parseInt(&#39;-0x32    &#39;));
<a name="line1073"></a>  assertEquals(-243, goog.string.parseInt(&#39;   -0xF3    &#39;));
<a name="line1074"></a>  assertTrue(isNaN(goog.string.parseInt(&#39; - 0x32 &#39;)));
<a name="line1075"></a>}
</pre>


</body>
</html>
